
#ifndef LOCAL_SIGN_HPP_
#define LOCAL_SIGN_HPP_

#include "utility.hpp"
#include "camera_config.h"

using namespace LfUtils;
using namespace LfCamStruct;

class Sign_Tracker;

struct LOCAL_SIGN {
    typedef float T;
    typedef cv::Rect_<T>   Rect;
    typedef cv::Size_<T>   Size;
    typedef cv::Point_<T>  Point;

    size_t id_;
    std::string category_;
    int type_;
    int color_;
    T score_;
    Rect bbox_;
    std::string imagename_;

    size_t global_id_;
    Rect calibrated_bbox_;
    Size img_sz_;
    T tau_; // width/height
    Sign_Tracker* tracker_;

    enum STATUS {
        NORMAL,
        NOISE,
        OVER_LAYPPED
    };
    STATUS status_ = NORMAL;

    /* Noise-Cirterial */
    const T TAU_RATIO = 3;
    const float BOUNDARY_RATIO = 0.01;

    /* debug related */
    cv::Scalar debug_color_; 
    bool debug_visualization_ = false;
    cv::Mat debug_img_;
    size_t frame_index_ = 0;
    Rect corrsponding_bbox_;

    void init(Camera_config &camera_config) {
        img_sz_ = camera_config.sz();
        calibrated_bbox_ = camera_config.undistortRect(bbox_);
        tau_ = cal_tau(calibrated_bbox_);
        
        // Identify Noise
        Rect imageRect(Point(0, 0), img_sz_);
        auto scaled_imageRect = scaling_rect(0.98, imageRect, img_sz_);
        Rect intersection = scaled_imageRect & calibrated_bbox_;
        float bbArea = calibrated_bbox_.area();
        float intersectDiff = std::abs(bbArea - intersection.area()) / bbArea;
        float WIDTH_CRITERIA  = BOUNDARY_RATIO * img_sz_.width;
        float HEIGHT_CRITERIA = BOUNDARY_RATIO * img_sz_.height;

        if ( calibrated_bbox_.width  < WIDTH_CRITERIA  ||
             calibrated_bbox_.height < HEIGHT_CRITERIA ||
             tau_ > TAU_RATIO || intersectDiff > 0.1
           ) 
        {
            status_ = NOISE;
        }
        tracker_ = nullptr;
    }
    bool is_noise()        { return status_ == NOISE; }
    bool is_overlaypped()  { return status_ == OVER_LAYPPED; }
    void set_overlaypped() { status_ = OVER_LAYPPED; }
};

#endif // LOCAL_SIGN_HPP_
